////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2008 Chris Colinsky
// All Rights Reserved
//
// version	0.4
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

import sugarcookie.events.Event;
import sugarcookie.logger.Logger;
import sugarcookie.utils.ObjectUtil;
import sugarcookie.collections.ArrayCollection;

import mx.events.EventDispatcher;
import flash.filters.BitmapFilter;

class sugarcookie.ui.UIMovieClip extends MovieClip {
	private var _log:Logger;
	private var _target:Object;
	private var _filter_lookup:ArrayCollection;
	private var events:ArrayCollection;
	
	public var filters:Array;
	public var id:Object;
	
	public static var BEVEL:String = "bevel";
	public static var BLUR:String = "blur";
	public static var COLOR_MATRIX:String = "colorMatrix";
	public static var CONVOLUTION:String = "convolution";
	public static var DISPLACEMENT_MAP:String = "displacementMap";
	public static var DROP_SHADOW:String = "dropShadow";
	public static var GRADIENT_BEVEL:String = "gradientBevel";
	public static var GRADIENT_GLOW:String = "gradientGlow";
	
	public var createEmptyMovieClip:Function;
	public var attachMovie:Function;
	public var removeMovieClip:Function;
	public var getNextHighestDepth:Function;
	public var onEnterFrame:Function;
	public var enabled:Boolean;
	public var _visible:Boolean;
	public var _alpha:Number;
	public var _name:String;
	public var _parent:MovieClip;
	public var _x:Number;
	public var _y:Number;
	public var _width:Number;
	public var _height:Number;
	
	public function UIMovieClip(t:Object) {
		EventDispatcher.initialize(this);
		_target = t!=undefined?t:this;
		_log = Logger.getLog();
		_filter_lookup = new ArrayCollection();
		events = new ArrayCollection();
	}
	
	public function addFilter(type:String, new_filter:BitmapFilter):Void {
		_filter_lookup.addObjWithKey(type, filters.length);
		var new_filter_set:Array = filters!=0?filters:[];
		new_filter_set.push(new_filter);
		filters = new_filter_set;
	}
	
	public function removeFilter(type:String):Void {
		_filter_lookup.removeItemAtKey(type);
	}
	
	public function getFilterIndex(type:String):Object {
		return _filter_lookup.getItemAtKey(type);
	}
	
	public function getFilterLength():Number {
		return filters.length;
	}
	
	public function postEvent(evt:String, msg:String, status:Boolean, isLogable:Boolean, data:Object):Void {
		var e:Event = new Event();
		e.target = _target;
		e.type   = evt;
		e.msg = msg!=undefined?msg:null;
		e.status = status!=undefined?status:null;
		e.data   = data!=undefined?data:null;
		
		if (isLogable) {
			var str:String = "Event - type: "+evt;
			if (msg.length>0 && msg!=null) str += ", msg: "+msg;
			if (status!=null) str += ", status: "+status;
			if (data!=null || data!=undefined) str += ", data:\n"+ObjectUtil.explode(data);
			_log.event(str);
		}
		
		dispatchEvent(e);
	}
	
	//this functionality currently only tracks one listener per event type
	public function addTrackableEventListener(type:String,listener:Object):Void {
		events.addObjWithKey(type,listener);
		addEventListener(type,listener);
	}
	
	public function removeTrackableEventListener(type:String,listener:Object):Void {
		events.removeItemAtKey(type);
		removeEventListener(type,listener);
	}
	
	public function hasEventListener(type:String):Boolean {
		return events.containsKey(type);
	}
	
	private function dispatchEvent() {};
	public  function addEventListener() {};
	public  function removeEventListener() {};
}